/**

 @Name：layuiAdmin（iframe版） 友链管理
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：LPPL

 */
layui.define(['admin', 'table'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element, form = layui.form;
    //全部数据
    tableIn = table.render({
        elem: '#list',
        url: '/shengteng/link/index',
        toolbar: '#toolbar',
        method: 'post',
        autoSort: false,
        cols: [[
            {checkbox: true, fixed: true},
            {field: 'id',align: 'center', title: 'ID', width: 80, fixed: true, sort: true},
            {field: 'title', title: '链接名称', width: 200,edit:'text'},
            {field: 'url', title: '链接URL', width: 300,templet:'#url',edit:'text'},
            {field: 'sort',align: 'center',title: '排序', width: 80, sort: true,edit:'text'},
            {field: 'open',align: 'center', title: '状态', width: 100, sort: true,toolbar: '#open'},
            {field: 'create_time', title: '添加时间', width: 180,sort: true,hide:true},
            {field: 'update_time', title: '修改时间', width: 180,sort: true},
            {width: 160, align: 'center', toolbar: '#action'}
        ]],
        page:true
    });
    //监听排序事件
    table.on('sort(list)', function(obj){
        table.reload('list', {
            initSort: obj
            ,where: {sort_by: obj.field,sort_order: obj.type}
        });
    });
    //修改字段
    table.on('edit(list)', function(obj){
        var param={};
        param[obj.field] = obj.value;
        param['id'] = obj.data.id;
        cf(param);
    });
    //改变会员状态
    form.on('switch(open)', function (obj) {
        var param={};
        param[obj.elem.name] = obj.elem.checked===true?1:0;
        param['id'] = this.value;
        cf(param);
    });
    function cf(param){
        admin.req({
            url: '/shengteng/link/cf',
            data: param,
            type:'post',
            success: function (res) {
                if(res.code==0){
                    layer.msg(res.msg,{time:1000,icon:1});
                }
                tableIn.reload();
            }
        });
    }
    //监听工具条
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        var id = data.id;
        if (obj.event === 'del') {
            layer.confirm('您确定要删除该友链吗？', function () {
                admin.req({
                    url: '/shengteng/link/del/'
                    , type: 'post'
                    , data: {'id': id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        }else if (obj.event === 'edit') {
            var index = layer.open({
                type: 2,
                content: '/shengteng/link/edit/id/' + id,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }
    });
    //编辑提交
    form.on('submit(edit-btn)', function (data) {
        admin.req({
            url: '/shengteng/link/edit/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });

    //搜索
    var active = {
        reload: function () {
            var key = $('#key').val();
            //执行重载
            table.reload('list', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    key: key
                }
            });
        }
    };

    //头工具栏事件
    table.on('toolbar(list)', function(obj){
        switch(obj.event){
            case 'add':
                var index = layer.open({
                    type: 2,
                    content: '/shengteng/link/add/',
                    area: ['300px', '300px'],
                    maxmin: true
                });
                layer.full(index);
                break;
            case 'direct':
                var checkStatus = table.checkStatus('list'),data = checkStatus.data; //获得选中的数据
                if(data.length === 0) return layer.msg('未选中行',{icon:2});
                var ids = [];
                $.each(data, function(i,val){
                    ids[i] = val['id'];
                });
                layer.confirm('确定删除所选友链吗？', function(){
                    admin.req({
                        url: '/shengteng/link/del/'
                        , type: 'post'
                        , data: {id:ids}
                        , success: function (res) {
                            layer.closeAll('dialog');
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
                break;
        }
    });
    //添加提交
    form.on('submit(add-btn)', function(data){
        admin.req({
            url: '/shengteng/link/add/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });
    $('.table-head-btn .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    exports('links', {});
});
